{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 初始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.edge.service import Service\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver import ActionChains\n",
    "from selenium.webdriver.common.by import By\n",
    "import time\n",
    "import random\n",
    "\n",
    "# 指定 Edge 驱动的完整路径\n",
    "driver_path = r\"D:\\点宽\\edgedriver_win64\\msedgedriver.exe\"\n",
    "\n",
    "# 创建 Service 对象\n",
    "service = Service(executable_path=driver_path)\n",
    "\n",
    "# 初始化 Edge 浏览器\n",
    "driver = webdriver.Edge(service=service)\n",
    "wait = WebDriverWait(driver, 10)\n",
    "PATH1 = \"https://digquant.com/account\"\n",
    "driver.get(PATH1)\n",
    "\n",
    "# 模拟人为输入\n",
    "def simulate_sendkeys(label, str1):\n",
    "    label.clear()\n",
    "    time.sleep(0.5)\n",
    "    for i in str1:\n",
    "        time.sleep(random.random())\n",
    "        label.send_keys(i)\n",
    "    time.sleep(0.5)\n",
    "    \n",
    "# 输入账号\n",
    "tag1 = driver.find_element(By.XPATH, \"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[1]/div/div/div/input\")\n",
    "simulate_sendkeys(tag1, '12345678901')\n",
    "\n",
    "# 输入密码\n",
    "tag2 = driver.find_element(By.XPATH, \"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[2]/div/div/div/input\")\n",
    "simulate_sendkeys(tag2, '123456')\n",
    "\n",
    "# 模拟鼠标点击\n",
    "time.sleep(1)\n",
    "action = ActionChains(driver)\n",
    "tag3 = driver.find_element(By.XPATH, '//*[@id=\"app\"]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[3]')\n",
    "action.click(tag3).perform()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 进阶"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "浏览器初始化成功\n",
      "已访问登录页面：https://www.digquant.com/account\n",
      "登录按钮已点击\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.edge.service import Service\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver import ActionChains\n",
    "from selenium.webdriver.common.by import By\n",
    "import time\n",
    "import random\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementClickInterceptedException\n",
    "\n",
    "# 初始化 Edge 浏览器\n",
    "def init_browser(driver_path):\n",
    "    try:\n",
    "        service = Service(executable_path=driver_path)\n",
    "        driver = webdriver.Edge(service=service)\n",
    "        driver.maximize_window()  \n",
    "        driver.implicitly_wait(5) \n",
    "        print(\"浏览器初始化成功\")\n",
    "        return driver\n",
    "    except Exception as e:\n",
    "        print(f\"浏览器初始化失败：{str(e)}\")\n",
    "        raise  \n",
    "\n",
    "# 模拟人为输入\n",
    "def simulate_human_input(element, input_str):\n",
    "    if not element.is_displayed() or not element.is_enabled():\n",
    "        raise ElementClickInterceptedException(f\"输入框不可交互：{element}\")\n",
    "    \n",
    "    element.clear()\n",
    "    time.sleep(random.random())  \n",
    "    \n",
    "    for char in input_str:\n",
    "        time.sleep(random.random())\n",
    "        element.send_keys(char)\n",
    "    \n",
    "    time.sleep(random.random())  \n",
    "\n",
    "# 登录过程\n",
    "def digquant_login(driver, login_url, username, password):\n",
    "    wait = WebDriverWait(driver, 10)  \n",
    "    \n",
    "    try:\n",
    "        # 访问登录页面\n",
    "        driver.get(login_url)\n",
    "        print(f\"已访问登录页面：{login_url}\")\n",
    "        time.sleep(random.uniform(1.5, 2.5)) \n",
    "        \n",
    "        # 定位账号\n",
    "        username_locator = (By.XPATH, \"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[1]/div/div/div/input\")\n",
    "        username_elem = wait.until(EC.visibility_of_element_located(username_locator))\n",
    "        simulate_human_input(username_elem, username)\n",
    "        \n",
    "        # 定位密码\n",
    "        password_locator = (By.XPATH, \"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[2]/div/div/div/input\")\n",
    "        password_elem = wait.until(EC.visibility_of_element_located(password_locator))\n",
    "        simulate_human_input(password_elem, password)\n",
    "        \n",
    "        # 定位登录按钮\n",
    "        login_btn_locator = (By.XPATH, '//*[@id=\"app\"]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[3]')\n",
    "        login_btn = wait.until(EC.element_to_be_clickable(login_btn_locator))\n",
    "        \n",
    "        # 模拟鼠标移动到按钮再点击\n",
    "        ActionChains(driver).move_to_element(login_btn).pause(random.uniform(0.5, 1.0)).click().perform()\n",
    "        print(\"登录按钮已点击\")\n",
    "        \n",
    "    except TimeoutException:\n",
    "        print(\"元素查找超时，可能是页面加载缓慢或元素定位失效\")\n",
    "        raise\n",
    "    except NoSuchElementException as e:\n",
    "        print(f\"未找到目标元素：{str(e)}\")\n",
    "        raise\n",
    "    except ElementClickInterceptedException as e:\n",
    "        print(f\"元素不可点击，可能被弹窗遮挡：{str(e)}\")\n",
    "        raise\n",
    "    except Exception as e:\n",
    "        print(f\"登录过程异常：{str(e)}\")\n",
    "        raise\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 配置参数\n",
    "    DRIVER_PATH = r\"D:\\点宽\\edgedriver_win64\\msedgedriver.exe\"\n",
    "    LOGIN_URL = \"https://www.digquant.com/account\"\n",
    "    USERNAME = \"12345678901\"\n",
    "    PASSWORD = \"123456\"\n",
    "    \n",
    "    # 登录流程执行\n",
    "    driver = None\n",
    "    try:\n",
    "        driver = init_browser(DRIVER_PATH)\n",
    "        digquant_login(driver, LOGIN_URL, USERNAME, PASSWORD)\n",
    "    except Exception as e:\n",
    "        print(f\"整体流程失败：{str(e)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "浏览器初始化成功\n",
      "已访问登录页面：https://www.digquant.com/account\n",
      "登录按钮已点击\n",
      "浏览器已关闭\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.edge.service import Service\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver import ActionChains\n",
    "from selenium.webdriver.common.by import By\n",
    "import time\n",
    "import random\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementClickInterceptedException\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "\n",
    "# 加载.env文件中的环境变量\n",
    "load_dotenv()\n",
    "\n",
    "# 初始化 Edge 浏览器\n",
    "def init_browser(driver_path):\n",
    "    try:\n",
    "        service = Service(executable_path=driver_path)\n",
    "        driver = webdriver.Edge(service=service)\n",
    "        driver.maximize_window()  \n",
    "        driver.implicitly_wait(5) \n",
    "        print(\"浏览器初始化成功\")\n",
    "        return driver\n",
    "    except Exception as e:\n",
    "        print(f\"浏览器初始化失败：{str(e)}\")\n",
    "        raise  \n",
    "\n",
    "# 模拟人为输入\n",
    "def simulate_human_input(element, input_str):\n",
    "    if not element.is_displayed() or not element.is_enabled():\n",
    "        raise ElementClickInterceptedException(f\"输入框不可交互：{element}\")\n",
    "    \n",
    "    element.clear()\n",
    "    time.sleep(random.random())  \n",
    "    \n",
    "    for char in input_str:\n",
    "        time.sleep(random.random())\n",
    "        element.send_keys(char)\n",
    "    \n",
    "    time.sleep(random.random())  \n",
    "\n",
    "# 登录过程\n",
    "def digquant_login(driver, login_url, username, password):\n",
    "    wait = WebDriverWait(driver, 10)  \n",
    "    \n",
    "    try:\n",
    "        # 访问登录页面\n",
    "        driver.get(login_url)\n",
    "        print(f\"已访问登录页面：{login_url}\")\n",
    "        time.sleep(random.uniform(1.5, 2.5)) \n",
    "        \n",
    "        # 定位账号\n",
    "        username_locator = (By.XPATH, \"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[1]/div/div/div/input\")\n",
    "        username_elem = wait.until(EC.visibility_of_element_located(username_locator))\n",
    "        simulate_human_input(username_elem, username)\n",
    "        \n",
    "        # 定位密码\n",
    "        password_locator = (By.XPATH, \"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[2]/div/div/div/input\")\n",
    "        password_elem = wait.until(EC.visibility_of_element_located(password_locator))\n",
    "        simulate_human_input(password_elem, password)\n",
    "        \n",
    "        # 定位登录按钮\n",
    "        login_btn_locator = (By.XPATH, '//*[@id=\"app\"]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[3]')\n",
    "        login_btn = wait.until(EC.element_to_be_clickable(login_btn_locator))\n",
    "        \n",
    "        # 模拟鼠标移动到按钮再点击\n",
    "        ActionChains(driver).move_to_element(login_btn).pause(random.uniform(0.5, 1.0)).click().perform()\n",
    "        print(\"登录按钮已点击\")\n",
    "        \n",
    "    except TimeoutException:\n",
    "        print(\"元素查找超时，可能是页面加载缓慢或元素定位失效\")\n",
    "        raise\n",
    "    except NoSuchElementException as e:\n",
    "        print(f\"未找到目标元素：{str(e)}\")\n",
    "        raise\n",
    "    except ElementClickInterceptedException as e:\n",
    "        print(f\"元素不可点击，可能被弹窗遮挡：{str(e)}\")\n",
    "        raise\n",
    "    except Exception as e:\n",
    "        print(f\"登录过程异常：{str(e)}\")\n",
    "        raise\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        # 从环境变量获取配置参数\n",
    "        driver_path = os.getenv(\"DRIVER_PATH\")\n",
    "        login_url = os.getenv(\"LOGIN_URL\")\n",
    "        username = os.getenv(\"USERNAME\")\n",
    "        password = os.getenv(\"PASSWORD\")\n",
    "        \n",
    "        # 验证必要的配置参数是否存在\n",
    "        if not all([driver_path, login_url, username, password]):\n",
    "            raise ValueError(\"请确保.env文件中包含所有必要的配置参数\")\n",
    "        \n",
    "        # 登录流程执行\n",
    "        driver = init_browser(driver_path)\n",
    "        digquant_login(driver, login_url, username, password)\n",
    "        \n",
    "        # 保持浏览器打开状态，方便观察结果\n",
    "        time.sleep(10)\n",
    "        \n",
    "    except Exception as e:\n",
    "        print(f\"整体流程失败：{str(e)}\")\n",
    "    finally:\n",
    "        if 'driver' in locals() and driver:\n",
    "            driver.quit()\n",
    "            print(\"浏览器已关闭\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
